package com.chenchen.tiguan.venue.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.chenchen.tiguan.common.utils.DateUtils;
import com.chenchen.tiguan.common.utils.SecurityUtils;
import com.chenchen.tiguan.venue.domain.Venue;
import com.chenchen.tiguan.venue.domain.VenueCourt;
import com.chenchen.tiguan.venue.domain.VenueOpenTime;
import com.chenchen.tiguan.venue.mapper.VenueCourtMapper;
import com.chenchen.tiguan.venue.mapper.VenueMapper;
import com.chenchen.tiguan.venue.mapper.VenueOpenTimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chenchen.tiguan.venue.mapper.VenueTimeSlotMapper;
import com.chenchen.tiguan.venue.domain.VenueTimeSlot;
import com.chenchen.tiguan.venue.service.IVenueTimeSlotService;

/**
 * 预生成的场地可预约时间段Service业务层处理
 * 
 * @author chenchen
 * @date 2025-04-14
 */
@Service
public class VenueTimeSlotServiceImpl implements IVenueTimeSlotService 
{
    @Autowired
    private VenueTimeSlotMapper venueTimeSlotMapper;
    @Autowired
    private VenueMapper venueMapper;
    @Autowired
    private VenueCourtMapper venueCourtMapper;
    @Autowired
    private VenueOpenTimeMapper venueOpenTimeMapper;

    /**
     * 查询预生成的场地可预约时间段
     * 
     * @param slotId 预生成的场地可预约时间段主键
     * @return 预生成的场地可预约时间段
     */
    @Override
    public VenueTimeSlot selectVenueTimeSlotBySlotId(Long slotId)
    {
        return venueTimeSlotMapper.selectVenueTimeSlotBySlotId(slotId);
    }

    /**
     * 查询预生成的场地可预约时间段列表
     * 
     * @param venueTimeSlot 预生成的场地可预约时间段
     * @return 预生成的场地可预约时间段
     */
    @Override
    public List<VenueTimeSlot> selectVenueTimeSlotList(VenueTimeSlot venueTimeSlot)
    {
        return venueTimeSlotMapper.selectVenueTimeSlotList(venueTimeSlot);
    }

    /**
     * 新增预生成的场地可预约时间段
     * 
     * @param venueTimeSlot 预生成的场地可预约时间段
     * @return 结果
     */
    @Override
    public int insertVenueTimeSlot(VenueTimeSlot venueTimeSlot)
    {
        venueTimeSlot.setCreateTime(DateUtils.getNowDate());
        venueTimeSlot.setCreateBy(SecurityUtils.getUsername());
        return venueTimeSlotMapper.insertVenueTimeSlot(venueTimeSlot);
    }

    /**
     * 修改预生成的场地可预约时间段
     * 
     * @param venueTimeSlot 预生成的场地可预约时间段
     * @return 结果
     */
    @Override
    public int updateVenueTimeSlot(VenueTimeSlot venueTimeSlot)
    {
        venueTimeSlot.setUpdateTime(DateUtils.getNowDate());
        venueTimeSlot.setUpdateBy(SecurityUtils.getUsername());
        return venueTimeSlotMapper.updateVenueTimeSlot(venueTimeSlot);
    }

    /**
     * 批量删除预生成的场地可预约时间段
     * 
     * @param slotIds 需要删除的预生成的场地可预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteVenueTimeSlotBySlotIds(Long[] slotIds)
    {
        return venueTimeSlotMapper.deleteVenueTimeSlotBySlotIds(slotIds);
    }

    /**
     * 删除预生成的场地可预约时间段信息
     * 
     * @param slotId 预生成的场地可预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteVenueTimeSlotBySlotId(Long slotId)
    {
        return venueTimeSlotMapper.deleteVenueTimeSlotBySlotId(slotId);
    }

    // 辅助方法：合并日期和时间
    private Calendar combineDateTime(Date date, Date time) {
        Calendar calDate = Calendar.getInstance();
        calDate.setTime(date);

        Calendar calTime = Calendar.getInstance();
        calTime.setTime(time);

        Calendar result = Calendar.getInstance();
        result.set(
                calDate.get(Calendar.YEAR),
                calDate.get(Calendar.MONTH),
                calDate.get(Calendar.DAY_OF_MONTH),
                calTime.get(Calendar.HOUR_OF_DAY),
                calTime.get(Calendar.MINUTE)
        );
        return result;
    }
    @Override
    public int updateVenueTimeSlotWhenInsertAndUpdate(Long venueId, VenueOpenTime venueOpenTime){
        Venue venue = venueMapper.selectVenueByVenueId(venueId);
        if(venue!=null){
            List<VenueCourt> venueCourtList = venueCourtMapper.selectVenueCourtListByVenueId(venueId);
            if(venueCourtList!=null){
                for (VenueCourt venueCourt : venueCourtList) {
                    venueTimeSlotMapper.deleteVenueTimeSlotByCourtId(venueCourt.getCourtId());
                    int i = 1;
                    if(venueOpenTime!=null){
                        Date startTime = venueOpenTime.getStartTime();
                        Date endTime = venueOpenTime.getEndTime();
                        Date startDay = venueOpenTime.getStartDay();
                        Date endDay = venueOpenTime.getEndDay();
                        Long slotDuring = venueOpenTime.getSlotDuring();
                    //遍历日期时间段
                        Calendar dayIterator = Calendar.getInstance();
                        dayIterator.setTime(startDay);
                        Calendar endDayCalendar = Calendar.getInstance();
                        endDayCalendar.setTime(endDay);

                        boolean after = dayIterator.after(endDayCalendar);
                        while (!dayIterator.after(endDayCalendar)) { // 遍历日期区间
                            Date currentDay = dayIterator.getTime();

                            // 初始化内层循环（时间段遍历）
                            Calendar timeIterator = Calendar.getInstance();
                            timeIterator.setTime(startTime); // 设置当天起始时间
                            Calendar endTimeCalendar = Calendar.getInstance();
                            endTimeCalendar.setTime(endTime);

                            while (!timeIterator.after(endTimeCalendar)) { // 遍历当日时间区间
                                Date slotStart = timeIterator.getTime();
                                System.out.println("日期：" + currentDay + "，时间段起点：" + slotStart);

                                // 根据业务逻辑处理每个时间段（例如生成预约槽位）
                                VenueTimeSlot venueTimeSlot = new VenueTimeSlot();
                                venueTimeSlot.setCourtId(venueCourt.getCourtId());
                                venueTimeSlot.setDate(currentDay);
                                venueTimeSlot.setStartTime(slotStart);
                                venueTimeSlot.setStatus(4L);
                                // 计算时间段结束时间
                                Calendar endTimeCal = Calendar.getInstance();
                                endTimeCal.setTime(slotStart);
                                endTimeCal.add(Calendar.MINUTE, slotDuring.intValue());
                                venueTimeSlot.setEndTime(endTimeCal.getTime());


                                //插入时间片
                                venueTimeSlotMapper.insertVenueTimeSlot(venueTimeSlot);
                                // 按分钟步长递增
                                timeIterator.add(Calendar.MINUTE, slotDuring.intValue());
                            }

                            // 递增一天
                            dayIterator.add(Calendar.DAY_OF_MONTH, 1);
                        }
                    }

                }
            }

        }
        return 0;
    }
    @Override
    public int updateVenueTimeSlotWhenDelete(Long venueId,VenueOpenTime venueOpenTime){
        Venue venue = venueMapper.selectVenueByVenueId(venueId);
        if(venue!=null){
            List<VenueCourt> venueCourtList = venueCourtMapper.selectVenueCourtListByVenueId(venueId);
            if(venueCourtList!=null){
                for (VenueCourt venueCourt : venueCourtList) {
                    venueTimeSlotMapper.deleteVenueTimeSlotByCourtId(venueCourt.getCourtId());
                }
            }
        }
        return 0;
    }

}
